﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IResultB;
using StartsEAS_API.Model;

namespace StartsEAS_API.Controllers.ResultContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class ResultController : ControllerBase
    {
        private readonly IResultBLL _resultBll;
        public ResultController(IResultBLL resultBLL) { 
            _resultBll = resultBLL;
        }
        /// <summary>
        /// 查询所有成绩信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllResults() {
            try
            {
                return _resultBll.GetAllResults();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllResults服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据班级查询成绩表
        /// </summary>
        /// <param name="token">教务处Token</param>
        /// <param name="classId">班级编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetResultsByClassId(string token, string classId) {
            try
            {
                return _resultBll.GetResultsByClassId(token, classId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetResultsByClassId服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据Token查询成绩表
        /// </summary>
        /// <param name="token">学生或老师token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetResultsByToken(string token) {
            try
            {
                return _resultBll.GetResultsByToken(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetResultsByToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 根据id查询成绩表信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetResultsById(int id) {
            try
            {
                return _resultBll.GetResultsById(id);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetResultsById服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
